<template>
  <div class="main">
    <div v-for="(component, index) in components" :key="index">
      <el-card v-if="component['type'] === 'el-card'" shadow="never" style="margin-top: 20px; margin-bottom: 20px">
        <template #header>
          <div slot="header" class="clearfix">
            <span>{{ component['label'] }}</span>
          </div>
        </template>
        <dynamic-form :components="component['components']" :formData="formData" />
      </el-card>

      <el-row v-else-if="component['type'] === 'el-row'" :gutter="20">
        <el-col v-for="(componentSub, subIndex) in component.components" :key="subIndex" :span="componentSub.span">
          <dynamic-form :components="[componentSub]" :formData="formData" />
        </el-col>
      </el-row>

      <div v-else-if="component['type'] === 'el-table'">
        <dynamic-table :component="component" :formData="formData" />
      </div>
      <div v-else-if="component['type'] === 'inline-text'">
        <inline-text-with-input :form-data="formData" :content="component.content" :fields="component.fields" />
      </div>

      <div v-else>
        <el-form-item
          :prop="component.field"
          :label="component.label"
          :label-position="component['label-position']"
          :label-width="component['label-width']">

          <component
            :is="component['type']"
            v-bind="component['props']"
            v-model="formData[component['field']]">
            <div v-if="component['type'] === 'el-radio-group'">
              <el-radio v-for="option in component.options" :value="option.value">
                {{ option.label }}
              </el-radio>
            </div>

            <div v-if="component['type'] === 'el-select'">
              <el-option
                v-for="option in component.options"
                :key="option.value"
                :label="option.label"
                :value="option.value"
              />
            </div>
          </component>
        </el-form-item>
      </div>
    </div>
  </div>
</template>

<script setup>
import DynamicTable from './DynamicTable.vue';
import InlineTextWithInput from './InlineTextWithInput.vue';

const { components, formData } = defineProps({
  components: {
    type: Array,
    required: true
  },
  formData: {
    type: Object,
    required: true
  }
});
</script>

<style scoped>

</style>
